package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.authentication.encoding.PlaintextPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppTblUser;
import com.irdstudio.apicenter.console.service.authenticate.AppTblUserService;
import com.irdstudio.apicenter.mobile.console.utils.SecurityUtil;


@Controller
@RequestMapping(value = "/appTblUser")
public class AppTblUserAction extends MacmsBaseAction {
	@Autowired
	private AppTblUserService appTblUserService;
	//private PasswordEncoder passwordEncoder = new PlaintextPasswordEncoder();

	// 插入跳转
	@RequestMapping(value = "jumpAddTblUser")
	public String jumpAddTblUser(HttpServletRequest request) {
	    String appid = request.getParameter("appid");
		request.setAttribute("appid", appid);
		return "sys/acl/user/addTblUser";
	}

	// 插入保存
	/*@RequestMapping(value = "addTblUser")
	public void addTblUser(@ModelAttribute("appTblUser") AppTblUser appTblUser,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		System.out.println(appTblUser.getId()+"==="+appTblUser.getIs_admin()+"==="+appTblUser.getPassword()+"==="+appTblUser.getRemark()+"==="+appTblUser.getLogin_name()+"===");
		if("0".equals(appTblUser.getIs_admin())){//管理员
			appTblUser.setIs_admin("Y");
			appTblUser.setIs_developer("N");
			appTblUser.setIs_configer("N");
		}else if("1".equals(appTblUser.getIs_admin())){//开发者
			appTblUser.setIs_admin("N");
			appTblUser.setIs_developer("Y");
			appTblUser.setIs_configer("N");
		}else{
			appTblUser.setIs_admin("N");
			appTblUser.setIs_developer("N");
			appTblUser.setIs_configer("Y");
		}
		flag = appTblUserService.insertOne(appTblUser);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}*/
	
	/**
	 * 插入保存
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addTblUser")
	public @ResponseBody
	ModelAndView addTblUser(
			@ModelAttribute("appTblUser") AppTblUser appTblUser)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		int flag = -1;
		System.out.println(appTblUser.getId()+"==="+appTblUser.getIs_admin()+"==="+appTblUser.getPassword()+"==="+appTblUser.getRemark()+"==="+appTblUser.getLogin_name()+"===");
		if("0".equals(appTblUser.getIs_admin())){//管理员
			appTblUser.setIs_admin("Y");
			appTblUser.setIs_developer("N");
			appTblUser.setIs_configer("N");
		}else if("1".equals(appTblUser.getIs_admin())){//开发者
			appTblUser.setIs_admin("N");
			appTblUser.setIs_developer("Y");
			appTblUser.setIs_configer("N");
		}else{
			appTblUser.setIs_admin("N");
			appTblUser.setIs_developer("N");
			appTblUser.setIs_configer("Y");
		}
		String pass = SecurityUtil.shaPassword(appTblUser.getPassword(), appTblUser.getLogin_name());
		appTblUser.setPassword(pass);
		flag = appTblUserService.insertOne(appTblUser);
		System.out.println("插入条数：flag="+flag);
		modelAndView.addObject("flag", flag);
		return modelAndView;
	}

	// 更新跳转
	@RequestMapping(value = "jumpUpdateTblUser")
	public String jumpUpdateTblUser(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		//String appid = request.getParameter("appid");
		AppTblUser appTblUser = appTblUserService.queryAppTradePrivate(recordKey);
		request.setAttribute("appTblUser", appTblUser);
		//request.setAttribute("appid", appid);
		return "sys/acl/user/updateTblUser";
	}
	
	// 更新跳转修改密码页面 
	@RequestMapping(value = "jumpUpdatePwd")
	public String jumpUpdatePwd(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		//String appid = request.getParameter("appid");
		AppTblUser appTblUser = appTblUserService.queryAppTradePrivate(recordKey);
		request.setAttribute("appTblUser", appTblUser);
		//request.setAttribute("appid", appid);
		return "sys/acl/user/updateTblUserPwd";
	}

	// 更新保存
	/*@RequestMapping(value = "updateTblUser")
	public void updateTblUser(@ModelAttribute("appTblUser") AppTblUser appTblUser,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		System.out.println(appTblUser.getId()+"==="+appTblUser.getIs_admin()+"==="+appTblUser.getPassword()+"==="+appTblUser.getRemark()+"==="+appTblUser.getLogin_name()+"===");
		if("0".equals(appTblUser.getIs_admin())){//管理员
			appTblUser.setIs_admin("Y");
			appTblUser.setIs_developer("N");
			appTblUser.setIs_configer("N");
		}else if("1".equals(appTblUser.getIs_admin())){//开发者
			appTblUser.setIs_admin("N");
			appTblUser.setIs_developer("Y");
			appTblUser.setIs_configer("N");
		}else{
			appTblUser.setIs_admin("N");
			appTblUser.setIs_developer("N");
			appTblUser.setIs_configer("Y");
		}
		//flag = appTblUserService.update(appTblUser);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}*/

	// 删除
	@RequestMapping(value = "deleteTblUser")
	public void deleteTblUser(HttpServletRequest request,
			HttpServletResponse response) throws SQLException,
			ClassNotFoundException {
		int flag = -1;
		String recordKey = request.getParameter("recordKey");
		flag = appTblUserService.delete(recordKey);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 查看
	@RequestMapping(value = "queryTblUserDetail")
	public String queryTblUserDetail(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		//String appid = request.getParameter("appid");
		AppTblUser appTblUser = appTblUserService.queryAppTradePrivate(recordKey);
		request.setAttribute("appTblUser", appTblUser);
		//request.setAttribute("appid", appid);
		return "sys/acl/user/queryTblUserDetail";
	}
	
	/**
	 * 更新保存
	 * 
	 * @param appTblUser
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "updateTblUser")
	public @ResponseBody
	ModelAndView updateTblUser(
			@ModelAttribute("appTblUser") AppTblUser appTblUser, HttpServletRequest request)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		int flag = -1;
		System.out.println(appTblUser.getId()+"==="+appTblUser.getIs_admin()+"==="+appTblUser.getPassword()+"==="+appTblUser.getRemark()+"==="+appTblUser.getLogin_name()+"===");
		if("0".equals(appTblUser.getIs_admin())){//管理员
			appTblUser.setIs_admin("Y");
			appTblUser.setIs_developer("N");
			appTblUser.setIs_configer("N");
		}else if("1".equals(appTblUser.getIs_admin())){//开发者
			appTblUser.setIs_admin("N");
			appTblUser.setIs_developer("Y");
			appTblUser.setIs_configer("N");
		}else{
			appTblUser.setIs_admin("N");
			appTblUser.setIs_developer("N");
			appTblUser.setIs_configer("Y");
		}
		flag = appTblUserService.update(appTblUser);
		if(flag > 0 && request.getSession().getAttribute("username").equals(appTblUser.getLogin_name())){
			System.out.println("================="+request.getSession().getAttribute("username"));
			request.getSession().setAttribute("isadmin", appTblUser.getIs_admin());
			request.getSession().setAttribute("isdeveloper", appTblUser.getIs_developer());
			request.getSession().setAttribute("isconfiger", appTblUser.getIs_configer());
			request.getSession().setAttribute("name", appTblUser.getName());
		}
		System.out.println("修改条数：flag="+flag);
		modelAndView.addObject("flag", flag);
		return modelAndView;
	}
	
	// 修改密码
	@RequestMapping(value = "updateTblUserPwd")
	public void updateTblUserPwd(@ModelAttribute("appTblUser") AppTblUser appTblUser,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		System.out.println(appTblUser.getId()+"==="+appTblUser.getPassword());
		String pass = SecurityUtil.shaPassword(appTblUser.getPassword(), appTblUser.getLogin_name());
		appTblUser.setPassword(pass);
		flag = appTblUserService.updateUserPwd(appTblUser);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}	

}

